
/*! 
	\file		vldheap.h
*	\brief		Provides internal new and delete operators.
*/
#pragma once

#ifndef VLDBUILD
#error \
"This header should only be included by Visual Leak Detector when building it from source. \
Applications should never include this header."
#endif

#include <windows.h>

#define GAPSIZE 4

/*!
	\struct	crtdbgblockheader_t
	\brief	Memory block header structure used internally by the debug CRT. All blocks
			allocated by the CRT are allocated from the CRT heap and, in debug mode, they
			have this header pretended to them (there's also a trailer appended at the
			end, but we're not interested in that).
*/
struct crtdbgblockheader_t
{
	struct crtdbgblockheader_t *next;       //!< Pointer to the next block in the list of blocks allocated from the CRT heap.
	struct crtdbgblockheader_t *prev;       //!< Pointer to the previous block in the list of blocks allocated from the CRT heap.
	char                    *file;          //!< Source file where this block was allocated.
	int                      line;          //!< Line of code, within the above file, where this block was allocated.
#ifdef _WIN64
	int                      use;           //!< This block's "use type": see below.
	size_t                   size;          //!< Size of the data portion of the block.
#else
	size_t                   size;          //!< Size of the data portion of the block.
	int                      use;           //!< This block's "use type":
#endif // _WIN64
#define CRT_USE_FREE     0                  //!<   This block has been freed.
#define CRT_USE_NORMAL   1                  //!<   This is a normal (user) block.
#define CRT_USE_INTERNAL 2                  //!<   This block is used internally by the CRT.
#define CRT_USE_IGNORE   3                  //!<   This block is a specially tagged block that is ignored during some debug error checking.
#define CRT_USE_CLIENT   4                  //!<   This block is a specially tagged block with special use defined by the user application.
	long                     request;       //!< This block's "request" number. Basically a serial number.
	unsigned char            gap [GAPSIZE]; //!< No-man's land buffer zone, for buffer overrun/underrun checking.
};

#define CRT_USE_TYPE(use) (use & 0xFFFF)	//!< Macro to strip off any sub-type information stored in a block's "use type".

/*!
	\struct	vldblockheader_t
	\brief	Memory block header structure used internally by VLD. All internally
			allocated blocks are allocated from VLD's private heap and have this header
			pretended to them.
*/
struct vldblockheader_t
{
	struct vldblockheader_t *next;          //!< Pointer to the next block in the list of internally allocated blocks.
	struct vldblockheader_t *prev;          //!< Pointer to the preceding block in the list of internally allocated blocks.
	const char              *file;          //!< Name of the file where this block was allocated.
	int                      line;          //!< Line number within the above file where this block was allocated.
	size_t                   size;          //!< The size of this memory block, not including this header.
	size_t                   serialNumber;  //!< Each block is assigned a unique serial number, starting from zero.
};

// Data-to-Header and Header-to-Data conversion
#define CRTDBGBLOCKHEADER(d) (crtdbgblockheader_t*)(((PBYTE)d) - sizeof(crtdbgblockheader_t))
#define CRTDBGBLOCKDATA(h) (LPVOID)(((PBYTE)h) + sizeof(crtdbgblockheader_t))
#define VLDBLOCKHEADER(d) (vldblockheader_t*)(((PBYTE)d) - sizeof(vldblockheader_t))
#define VLDBLOCKDATA(h) (LPVOID)(((PBYTE)h) + sizeof(vldblockheader_t))

// new and delete operators for allocating from VLD's private heap.
void operator delete (void *block);
void operator delete [] (void *block);
void operator delete (void *block, const char *file, int line);
void operator delete [] (void *block, const char *file, int line);
void* operator new (size_t size, const char *file, int line);
void* operator new [] (size_t size, const char *file, int line);

// All calls to the new operator from within VLD are mapped to the version of
// new that allocates from VLD's private heap.
#define new new(__FILE__, __LINE__)
